<?php
/*
 * Created on Oct 12, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<script type="text/javascript">
$(document).ready(function() {
	$('#addNewsletter').click(function() {
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 580,
			width: 650,
			title: 'Create News',
			modal: true,
			buttons: {
				'Create': function() {
					// Validation
					$('#newsletterForm').ajaxForm({
						success: function(data){
							if(data.status=="success"){
								alert("You have just insert new record.");
								var $table = $("#news_list tbody");
								$.ajax({
								  url: "<?=site_url(array('c'=>'cms_news','m'=>'get_newest_news'))?>",
								  cache: false,
								  success: function(html){
								    $table.append(html);
								  }
								});
							}
							$("#dialog").dialog('close');
						},
						dataType: 'json'
					});
		            $('#newsletterForm').submit();
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				$('#newsletterGroupForm input').val('').removeClass('ui-state-error');
			}
		});
		$('#dialog').load($(this).attr('href'));
		$("#dialog").dialog("open");
		return false;
	});

	$("a.ajax-edit").click(function(){
		$('#dialog').load($(this).attr('href'));
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 620,
			width: 650,
			title: 'Edit News',
			modal: true,
			buttons: {
				'Save': function() {
					// Validation
					$('#newsletterForm').ajaxForm({
						beforeSubmit: function(){
							// show waiting
						},
						success: function(data){
							if(data.status=="success"){
								$("#"+data.id + " .item_name").html(data.item_name);
								$("#"+data.id + " .item_postdate").html(data.item_postdate);
								$("#"+data.id + " .item_group").html(data.item_group);
								$("#"+data.id + " .item_sort").html(data.item_sort);
								$("#"+data.id + " .item_image img").attr("src","<?=userfile_img_url('')?>"+data.item_img);
								alert("You have just updated a record.");
								
							}
						},
						dataType: 'json'
					});
		            $('#newsletterForm').submit();
					$("#dialog").dialog('close');
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				$('#newsletterGroupForm input').val('').removeClass('ui-state-error');
			}
		});
		$("#dialog").dialog("open");
		return false;
	});

	$("a.ajax-delete").click(function(){
		var delLink = $(this).attr("href");
		var temp = new Array(); 
		temp = delLink.split('/');
		id = temp[temp.length-1];
		$("#dialog").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This item will be permanently deleted and cannot be recovered. Are you sure?</p>')
		$("#dialog").dialog({
			title: 'Delete "' + $(this).attr('title') + '"',
			autoOpen: true,
			height: 180,
			buttons: {
				'Delete': function() {
					// Submit delete item
					$.get(delLink, function(){
						alert("You have just removed a record.");
					}, "json");
					$("#"+id).remove();
					$(this).dialog('close');
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			}
		});
		$("#dialog").dialog("open");
		return false;
	});

	// Initialize tooltip
	$(".dyna[title]").tooltip({
		// use single tooltip element for all tips
		tip: '#dynatip',
		position: 'top center',
		offset: [-60, 0],
		// use "slide" effect
		effect: 'slide'
		// add dynamic plugin
	});

	$("#newsletterSearchAction").submit(function() {
		if($("#actions").val()==""){
			alert("You must chose one action, please!");
			return false;
		}
		if(hasChecked("input.newsletterId")){
			// Check what does Actions Option selected
			var actions = $("#actions").val();
			if(actions == "sendNewsletter"){
				return true;
			}
			if(actions == "update2Group"){
				// Check group selected
				if($("#group4Update").val() != ""){
					return true;
				}
				else{
					alert("You must select Group, please!");
				}
			}
		}
		else{
			alert("You must check at least one Checkbox, please!");
		}
		return false;
	});

	$("#actions").bind("change", function(){
		if($(this).val() == "update2Group"){
			// Show group
			$("#groupPanel").show();
		}
		else{
			$("#groupPanel").hide();
		}
	});

	function hasChecked(objCheck){
		if($(objCheck + ":checked").length > 0){
			return true;
		}
		else{
			return false;
		}
	}

	$("#f_rangeStart").datepicker({dateFormat: 'yy-mm-dd'});
	$("#t_rangeStart").datepicker({dateFormat: 'yy-mm-dd'});
	
	
	$("#search").click(function(){
		keyword = $("input[name='keyword']").attr('value');
		group = $("select[name='group']").attr('value');
		per_page = $("select[name='limit']").attr('value');
		form = $("input[name='fromdate']").attr('value');
		to = $("input[name='todate']").attr('value');
		t = $.post("<?=site_url(array('c'=>'cms_news','m'=>'browse_newsletter'))?>",
					 {keyword: keyword, group: group, search: 1, limit: per_page, fromdate: form, todate: to},
					 function(data){
					 	$("#ui-tabs-2").html(data);
					 });
		return false;
	});
	
	$(".pagination a").click(function(){
		url = $(this).attr('href');
		var temp = new Array(); 
		temp = url.split('/');
		limit = temp[temp.length-1];
		
		keyword = $("input[name='keyword']").attr('value');
		group = $("select[name='group']").attr('value');
		per_page = $("select[name='limit']").attr('value');
		form = $("input[name='fromdate']").attr('value');
		to = $("input[name='todate']").attr('value');
		t = $.post(url,
					 { keyword: keyword, group: group, search: 1, limit: per_page, fromdate: form, todate: to},
					 function(data){
					 	$("#ui-tabs-2").html(data);
					 } 
		);
		
		return false;	
	});
	
});
</script>
<div class="content-inner">
	<form action="" method="post" id="newsletterSearch">
	<div class="form-search">
		<table class="" cellpadding="5" cellspacing="0" style="">
			<tbody>
				<tr>
					<td>Keyword </td>
					<td><input type="text" name="keyword" value="<?=is_array($search) ? $search['keyword'] : ''?>" style="width: 230px;"/> </td>
					<td>Group </td>
					<td><select name="group" style="width: 234px;">
						<option value="">All</option>
						<?php foreach($groups as $item):?>
						<option value="<?=$item['id']?>" <?=is_array($search) && ($search['group'] == $item['id'])? 'selected="true"' : ''?>>
							<?=$item['name']?>
						</option>
						<?php endforeach;?>
						</select>
					</td>
					<td rowspan="2"><input name="search" type="button" class="" value="Search" id="search" /></td>
				</tr>
				<tr>
					<td>Per Page </td>
					<td><select name="limit" style="width: 234px;" class="styled">
							<?php $number = 0;while($number < 200): $number += 20;?>
							<option value="<?=$number?>" <?=is_array($search) && ($search['limit'] == $number)? 'selected="true"' : ''?>><?=$number?></option>
							<?php endwhile;?>
						</select>
					</td>
					<td>From </td>
					<td>
						<input type="text" id="f_rangeStart" name="fromdate" value="<?=is_array($search) ? $search['fromdate'] : ''?>" maxlength="10" size="14"/>
						&nbsp;to&nbsp;
						<input type="text" id="t_rangeStart" name="todate" value="<?=is_array($search) ? $search['todate'] : ''?>" maxlength="10" size="14"/>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
	</form>
</div>
<br/>
<div class="content-inner">
	<form action='<?=site_url(array("c" =>"cms_news", "m"=>"browse_newsletter"))?>' method="post" id="newsletterSearchAction">
	<div id="dynatip"> </div>
	<div style="text-align: right;">
		<a id="addNewsletter" href="<?=site_url(array("c" => "cms_news", "m" => "add_newsletter"))?>">
			<img src="<?=image_url();?>function/add.gif" width="106" height="31" border="0">
		</a>
	</div>
	<table class="cont-table-list" cellpadding="5" cellspacing="0" style="width: 100%;" id="news_list">
		<thead>
			<tr>
				<th class="mini"><input type="checkbox" name="checkAll" value="0" onclick="checkAllItem('newsletterId', this);"/></th>
				<th class="small">Edit</th>
				<th class="small">Delete</th>
				<th>Group</th>
				<th>Image</th>
				<th>Name</th>
				<th>Post Date</th>
				<th class="small">Sort</th>
			</tr>
		</thead>
		<tbody>
			<?php if(is_array($newsletters)): $i = 0; $per_page = $this->pagination->cur_page * $this->pagination->per_page; ?>
			<?php foreach($newsletters as $item): $i++?>
			<tr class="<?= $i%2 ? 'odd' : 'even'?>" id="<?=$item['id']?>">
				<td class="small"><input type="checkbox" class="newsletterId" name="newsletterIds[]" value="<?=$item['id']?>"/></div>
				<td class="small"><a class="ajax-edit" href="<?=site_url(array("c" => "cms_news", "m" => "edit_newsletter", "id"=>$item['id']))?>" alt="Edit"><img src="<?=image_url()?>icons/edit.gif" alt="Edit"/></a></td>
				<td class="small"><a class="ajax-delete" href="<?=site_url(array("c" => "cms_news", "m" => "delete_newsletter", "id"=>$item['id']))?>" title="<?=$item['name']?>" alt="Delete"><img src="<?=image_url()?>icons/del.gif" alt="Delete"/></a></td>
				<td class="item_group"><?=$item['group_name']?></td>
				<td class="item_image">
					<?php if(!empty($item['img'])):?>
					<img src="<?=$item['img']?>" style="max-width: 220px;"/>
					<?php endif;?>
				</td>
				<td class="item_name"><a class="dyna" href="#" title="<?=htmlspecialchars($item['description'])?>"><?=$item['name']?></a></td>
				<td class="item_postdate"><?=$item['postdate']?></td>
				<td class="item_sort"><?=$item['sort']?></td>
			</tr>
			<?php endforeach;?>
			<?php else:?>
			<tr>
				<td colspan="8" align="center">Not exist items</td>
			</tr>
			<?php endif;?>
		</tbody>
	</table>
	<div style="position: absolute; padding-left: 20px;">
		<label>Actions: </label>
		<select name="actions" id="actions" class="required">
			<option value="">Select one</option>
			<option value="delete">Delete</option>
			<option value="sendNewsletter">Send Newsletter</option>
			<option value="update2Group">Update To Group</option>
		</select>
		<span id="groupPanel" style="display:none;">
			<label>Group: </label>
			<select id="group4Update" name="groupId" style="width: 150px;">
				<option value="">Select one</option>
				<?php foreach($groups as $item):?>
				<option value="<?=$item['id']?>">
					<?=$item['nam']?>
				</option>
				<?php endforeach;?>
			</select>
		</span>
		<input type="submit" name="submit" value="Submit" />
	</div>
	<div class="pagination">
	<span>Result: <?=$this->pagination->total_rows?> items</span>
	<?= $this->pagination->create_links();?>
	</div>
</div>
</form>
